package com.qiantu.phone.ui.shotview;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.phone.BuildConfig;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.bean.AreaBean;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UniversalShotView extends BaseDeviceActView {
    private WebView deviceWebview;

    public UniversalShotView(Context context) {
        super(context);
    }

    public void setDevice(DeviceBean device){
        mDevice = device;
    }

    @Override
    public int getLayoutId() {
        return R.layout.universal_shot_view;
    }

    @Override
    public void initView() {
        deviceWebview = findViewById(R.id.universal_webview);
    }

    @Override
    public void updateView() {
        this.deviceType = mDevice.getDeviceType();
        this.type = mDevice.getType();
        String deviceControl = "";
        if(mDevice.getDeviceType().equals("universal_freshair")){
            deviceControl = "freshairSence";
        }else if(mDevice.getDeviceType().equals("universal_sequencer")){
            deviceControl = "sequenceSence";
        }else if(mDevice.getDeviceType().equals("universal_projector")){
            deviceControl = "projectorSence";
        }else if(mDevice.getDeviceType().equals("universal_player")){
            deviceControl = "playSence";
        }else if(mDevice.getDeviceType().equals("universal_pa")){
            deviceControl = "powerAmplifierSence";
        }
        WebSettings webSettings = deviceWebview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        String url = BuildConfig.DEVICE_URL + deviceControl + "/?token="+ AppApplication.getInstance().getLoginInfo().getToken() +"&houseId="+ AppApplication.getInstance().getCurHouseInfo().getHouseSerialNo() + "&deviceSerialNo=" + mDevice.getDeviceSerialNo() + "&phone="+ AppApplication.getInstance().getUserPhone()+"&phoneType=android";
//        String url = "www.baidu.com";
        deviceWebview.loadUrl(url);
        //处理各种通知请求和事件，如果不使用该句代码，将使用内置浏览器访问网页
        deviceWebview.setWebViewClient(new WebViewClient());
        deviceWebview.setVerticalScrollBarEnabled(true);
        deviceWebview.addJavascriptInterface(new Object(){
            @JavascriptInterface
            public void functionAction(String message) {
                // 显示一个Toast消息
                Log.d("functionAction", message);
                Handler mainHandler = new Handler(Looper.getMainLooper());

                mainHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        addAct(message);
                    }
                });

            }
        },"android");
    }

    private void addAct(String  message){
        Gson gson = new Gson();
        Type listType = new TypeToken<Map<String,Object>>() {}.getType();
        Map<String,Object> data = gson.fromJson(message, listType);
        Map<String,Object> params = (Map<String, Object>) data.get("params");
        Map<String,Object> action = (Map<String, Object>) params.get("deviceAction");
        if(mDevice.getDeviceType().equals("universal_sequencer")){
            action.put("rangeName",String.format("%s:%s",action.get("combineName"),action.get("rangeName")));
        }
        addAct((String) action.get("actionIdentifier"), action);
    }

}